package com.dd.ms.log.web.pojo;

import com.dd.ms.common.pojo.annotation.Explanation;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.*;

import java.io.Serializable;
import java.time.Instant;

/**
 * @Author liuxianmeng
 * @CreateTime 2025/6/28 15:02
 * @Description 日志ESDO 对应ES数据库
 */
/**
 * 日志ESDO 对应ES数据库
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "log-entries-2025")
public class LogEntryESDO implements Serializable {

    @Id
    @Explanation(value = "ES自动生成的文档ID")
    private String id;

    @Field(type = FieldType.Date, format = DateFormat.date_optional_time)
    @Explanation(value = "时间戳(ISO8601格式)")
    private Instant timestamp;

    @Field(type = FieldType.Keyword)
    @Explanation(value = "日志级别 枚举 ERROR/WARN/INFO/DEBUG")
    private String level;

    @Field(type = FieldType.Keyword)
    @Explanation(value = "服务名")
    private String serviceName;

    @Field(type = FieldType.Keyword)
    @Explanation(value = "主机IP")
    private String hostIp;

    @Field(type = FieldType.Keyword)
    @Explanation(value = "分布式追踪ID")
    private String traceId;

    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    @Explanation(value = "日志内容(支持中文分词)")
    private String message;

    @Field(type = FieldType.Object)
    @Explanation(value = "错误详情(ERROR级时使用)")
    private ErrorDetail errorDetail;

    @Field(type = FieldType.Long)
    @Explanation(value = "响应耗时(毫秒)")
    private Long durationMs;

    @Getter
    @Setter
    @Builder
    public static class ErrorDetail {
        @Field(type = FieldType.Keyword)
        private String type;  // SYSTEM/BUSINESS

        @Field(type = FieldType.Keyword)
        private String code;

        @Field(type = FieldType.Text, index = false)
        private String stackTrace;
    }
}
